package beans;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ConfigBean implements Serializable{
	
	private static final long serialVersionUID = -3952662173007873764L;
	int listenerport;
	int dbport;
	String dbuname;
	String dbpassword;
	String fileLocation;
	String fileName;
	
	public ConfigBean() {
		
		if ( System.getProperty( "os.name" ).toLowerCase( ).startsWith( "windows" )) {
			fileName = "\\serverconfig.dat";
		}
		else {
			fileName = "/serverconfig.dat";
		}
		
		if ( System.getProperty( "os.name" ).toLowerCase( ).startsWith( "windows" )) {
			fileLocation = "C:\\Program Files\\Villicus";
		}
		else {
			fileLocation = "/Library/Villicus";
		}
		
		File file = new File(fileLocation);
		if (!file.exists()) {
			file.mkdirs();
		}
		
		try {
			FileInputStream input = new FileInputStream(fileLocation+fileName);
			ObjectInputStream objInput = new ObjectInputStream(input);
			Object obj = objInput.readObject();
			if (obj instanceof ConfigBean) {
				ConfigBean imported = (ConfigBean) obj;
				this.listenerport = imported.getListenerport();
				this.dbport = imported.getDbport();
				this.dbuname = imported.getDbuname();
				this.dbpassword = imported.getDbpassword();
			}
		} catch (Exception e) {
			this.listenerport = 0;
			this.dbport = 0;
			this.dbuname = "";
			this.dbpassword = "";
		}
		
		
	}

	
	
	public int getListenerport() {
		return listenerport;
	}



	public void setListenerport(int portNumber) {
		this.listenerport = portNumber;
	}



	public int getDbport() {
		return dbport;
	}



	public void setDbport(int dbport) {
		this.dbport = dbport;
	}



	public String getDbuname() {
		return dbuname;
	}



	public void setDbuname(String dbuname) {
		this.dbuname = dbuname;
	}



	public String getDbpassword() {
		return dbpassword;
	}



	public void setDbpassword(String dbpassword) {
		this.dbpassword = dbpassword;
	}



	public void saveBean() {
		
		try {
			FileOutputStream output = new FileOutputStream(fileLocation+fileName);
			ObjectOutputStream objOutput = new ObjectOutputStream(output);
			objOutput.writeObject(this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
